package com.nnfe.fyd.utils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.nnfe.fyd.R;
import com.nnfe.fyd.base.MyBaseAdapter;

import java.util.List;

/**
 * ----------------------------------
 * Created by xyd on 2017/3/21.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class PopupViewUtil {

    private static PopupWindow popWindow;

    /**
     * @param mContext
     * @param showUnder         // 显示在此view之下
     * @param listAdapter       //
     * @param dataList          //
     * @param itemClickListener // itemClickListener
     */
    public static void showListPopupView(Activity mContext, View showUnder, MyBaseAdapter listAdapter, List dataList, AdapterView.OnItemClickListener itemClickListener) {

        if (popWindow != null) {
            //LogUtil.e("xyd", "popWindow show state" + popWindow.isAboveAnchor());
        }
        if (popWindow != null && popWindow.isShowing()) {
            popWindow.dismiss();
            return;
        }

        ListView listView = (ListView) LayoutInflater.from(mContext).inflate(R.layout.popupwindow_listview, null, false);
        listView.setBackgroundResource(R.drawable.edit_frame);
        listAdapter.resetData(dataList);


        //1.构造一个PopupWindow，参数依次是加载的View，宽高
        popWindow = new PopupWindow(listView,
                showUnder.getWidth(), WindowManager.LayoutParams.WRAP_CONTENT, false);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);
        popWindow.showAsDropDown(showUnder);


        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(itemClickListener);
    }


    public static void dismiss() {
        if (popWindow != null) {
            popWindow.dismiss();
        }
    }
}
